﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MetroDataApi.Models
{
    public class SinglePointRecentChanes
    {
        public static IEnumerable<result> RecentChanges(int prjId, string ptName, string queryType = "", string startTime = "", string endTime = "")
        {
            var list = new List<result>();
            var oTime = QueryResults.GetOriginTime(prjId, ptName);
            var oValue = QueryResults.GetResultsByTime(prjId, oTime, new List<string>() { ptName }).FirstOrDefault();
            if (oValue == null) return list;
            switch (queryType)
            {
                case "custom":
                    {
                        return SinglePointRecentChanes.RecentTimesChangesBySpecifyTimeList(prjId, ptName, oValue.value, startTime, endTime);
                    }
                default: //默认取最近10次
                    {
                        return SinglePointRecentChanes.RecentTimesChanges(prjId, ptName, oValue.value);
                    }
            }
        }

        private static List<result> RecentTimesChangesBySpecifyTimeList(int prjId, string ptName, double oValue, 
                    string startTime, string endTime)
        {
            var startDate = DateTime.Parse(startTime);
            var endDate = DateTime.Parse(endTime);
            //防止用户是个傻逼,时间输反了
            if (endDate < startDate)
            {
                var temp = endDate;
                endDate = startDate;
                startDate = temp;
            }
            using (var db = new MetroDataDataContext())
            {
                var listPt = (from p in db.MonitorPoints
                              where p.PrjId == prjId && p.Name == ptName && p.DateTime >= startDate && p.DateTime <= endDate
                              orderby p.DateTime
                              select p).ToList();
                var list = listPt.Select(p => new result()
                {
                    name = ptName,
                    time = p.DateTime.Value.ToString("yyyy-MM-dd"),
                    value = Math.Round((p.Value.Value - oValue) * 1000, 2, MidpointRounding.AwayFromZero)
                }).ToList();
                return list;
            }

        }


        public static List<result> RecentTimesChanges(int prjId, string ptName, double oValue, int howMany = 10)
        {
            using (var db = new MetroDataDataContext())
            {
                var listPt = (from p in db.MonitorPoints
                            where p.PrjId == prjId && p.Name == ptName
                            orderby p.DateTime descending
                            select p).Take(howMany).ToList();
                var list = (from p in listPt
                            orderby p.DateTime ascending
                            select new result()
                            {
                                name = ptName,
                                time = p.DateTime.Value.ToString("yyyy-MM-dd"),
                                value = Math.Round((p.Value.Value - oValue) * 1000, 2)
                            }).ToList();
                return list;
            }
        }
    }
}